package com.eudon.ai.agent.etl.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.autoconfigure.vectorstore.pgvector.PgVectorStoreProperties;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author eudon
 * @description 自定义pgvector
 * @date 2025/8/15
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({PgVectorStoreProperties.class})
public class MyPostgresVectorConfig {
    @PostConstruct
    public void init() {
        log.info("MyPostgresVector init");
    }

    @Bean
    public VectorStore myPostgresVectorStore(JdbcTemplate jdbcTemplate,
                                             EmbeddingModel dashScopeEmbeddingModel,
                                             PgVectorStoreProperties properties) {

        return PgVectorStore.builder(jdbcTemplate, dashScopeEmbeddingModel)
                .distanceType(properties.getDistanceType())
                .initializeSchema(true)
                .idType(PgVectorStore.PgIdType.BIGSERIAL)
                .schemaName(properties.getSchemaName())
                .vectorTableName(properties.getTableName())
                .maxDocumentBatchSize(properties.getMaxDocumentBatchSize())
                .removeExistingVectorStoreTable(true)
                .build();

    }
}
